﻿#include "AudioUtil.h"
#include "AudioEngine.h"
using namespace experimental;

AudioUtil* AudioUtil::instence = nullptr;

/*
	实现单例
*/
AudioUtil* AudioUtil::getInstence() {
	srand(time(0));
	int bgmNum = rand() % 3;
	if (instence == nullptr) {
		instence = new AudioUtil(bgmNum);
	}
	return instence;
}

AudioUtil::AudioUtil(int bgmNum):
	bgmId(-1),
	bgmNum(bgmNum)
{
	
}

/*
	播放背景音乐
*/
void AudioUtil::playBgm() {
	//随机从三个BGM中选一个播放
	if (bgmId == -1) {
		//AudioEngine::uncacheAll();
		char filename[30];
		sprintf_s(filename, "sound/PlayBg_%d.mp3", bgmNum);
		bgmId = AudioEngine::play2d(filename, true, 0.1);
		//AudioEngine::setVolume(bgmId, 0.1);
	} else {
		AudioEngine::resume(bgmId);
	}
}

/*
	停止背景音乐
*/
void AudioUtil::stopBgm(bool pauseBgm) {
	if (bgmId != -1) {
		if (pauseBgm != false) {
			AudioEngine::pause(bgmId);
		} else {
			AudioEngine::stop(bgmId);
			bgmId = -1;
		}
	} 
}

/*
	按钮点击声音
*/
void AudioUtil::buttonClickSound() {
	AudioEngine::play2d("sound/button.mp3", false, 0.3);
	//AudioEngine::setVolume(AudioEngine::play2d("sound/click.mp3"), 0.1);
}

/*
	敌机射击声音
*/
void AudioUtil::enemyShootSound() {
	//AudioEngine::setVolume(AudioEngine::play2d("sound/button.mp3"), 0.1);
}

/*
	英雄射击声音
*/
void AudioUtil::heroShoottSound() {
	AudioEngine::play2d("sound/heroShoot.mp3", false, 0.01);
}

/*
	僚机射击声音
*/
void AudioUtil::wingAirShoot() {
	//AudioEngine::setVolume(AudioEngine::play2d("sound/wingAirShoot.mp3"), 0.1);
}

//病毒死亡音效
void AudioUtil::enemyDesyEff() {
	AudioEngine::play2d("sound/enemyBlast.mp3", false, 0.3);
}

//获取金币音效 
void AudioUtil::getCoinSound() {
	AudioEngine::setVolume(AudioEngine::play2d("sound/getCoin.mp3"), 0.1);
}

